var body;
var outputs = new Array();

var maxReports   = 0;
var reportOffset = 0;
var reportAmount = 10;


function renderTemplates(xml) {
	var templates = xml.getElementsByTagName("template");
	var tbody = $("templates_table").getElementsByTagName("tbody").item(0);

	for (var i=0; i<templates.length; i++) {

		var title 	= templates[i].getAttribute("title");
		var filename 	= templates[i].getAttribute("filename");
		var changed	= templates[i].getAttribute("changed");
		var description = templates[i].firstChild.nodeValue;

		var tr = document.createElement("tr");

		//Title
		var td1 = document.createElement("td");
		td1.appendChild(document.createTextNode(title));

		//Description
		var td2 = document.createElement("td");
		td2.appendChild(document.createTextNode(description));

		//Last changed
		var td3 = document.createElement("td");
		td3.appendChild(document.createTextNode(changed));

		//Actions
		var td4 = document.createElement("td");
		td4.style.textAlign = "right";

		//---> Run
		var runA = document.createElement("a");
		runA.href = "./?page=template_executor&run="+filename;
		runA.style.padding = "4px";
		var runImg = document.createElement("img");
		runImg.src = "img/silk/report_go.png";
		runImg.style.border = "0px";
		runImg.alt = "Run Report Template";
		runA.appendChild(runImg);
		td4.appendChild(runA);

		//---> Edit
		var editA = document.createElement("a");
		editA.href = "./?page=template_builder&template="+filename;
		editA.style.padding = "4px";
		var editImg = document.createElement("img");
		editImg.src = "img/silk/report_edit.png";
		editImg.style.border = "0px";
		editImg.alt = "Edit Report Template";
		editA.appendChild(editImg);
		td4.appendChild(editA);

		//---> Delete
		var deleteA = document.createElement("a");
		deleteA.href = "javascript:;";
		deleteA.style.padding = "4px";
		var deleteImg = document.createElement("img");
		deleteImg.src = "img/silk/delete.png";
		deleteImg.style.border = "0px";
		deleteImg.alt = "Delete Report Template";
		deleteImg.setAttribute("filename", filename);
		deleteA.appendChild(deleteImg);
		td4.appendChild(deleteA);

		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);

		tbody.appendChild(tr);


		Event.observe(deleteA, "click", confirmDeleteTemplate);
	}

	//Force tooltip parsing
	parse_tooltips();
}



function clearReports() {
	var tbody = $("reports_table").getElementsByTagName("tbody").item(0);

	for (var i=0; i<tbody.childNodes.length; i++) {
		var node = tbody.childNodes[i];
		var id	 = null;
		if (typeof(node.getAttribute) == "function")
			id = node.getAttribute("id");

		if (id != "header_row") {
			tbody.removeChild(node);
			i--;
		}
	}
}

function renderReports(xml) {
	var reports = xml.getElementsByTagName("report");
	var tbody = $("reports_table").getElementsByTagName("tbody").item(0);

	for (var i=0; i<reports.length; i++) {

		var title 	= reports[i].getAttribute("title");
		var filename 	= reports[i].getAttribute("filename");
		var time	= reports[i].getAttribute("generated-on");
		var builtIn	= parseFloat(reports[i].getAttribute("built-in"));


		var tr = document.createElement("tr");

		//Title
		var td1 = document.createElement("td");
		td1.appendChild(document.createTextNode(title));

		//Parameters
		var td2 = document.createElement("td");
		td2.appendChild(document.createTextNode("-"));

		//Generated on
		var td3 = document.createElement("td");
		td3.appendChild(document.createTextNode(time));

		//Built in
		var td4 = document.createElement("td");
		var builtInStr = "n/a";
		if (builtIn >= 0) {
			//Round to two decimals
			builtInStr = (Math.round(builtIn*100)/100) + "s";
		}
		td4.appendChild(document.createTextNode(builtInStr));

		//Formats
		var td5 = document.createElement("td");

		for (var o=0; o<outputs.length; o++) {
			if (o>0) {
				td5.appendChild(document.createTextNode(", "));
			}
			var a = document.createElement("a");
			a.href = "api_get_report.php?type="+outputs[o]+"&file="+filename;
			a.target = "_BLANK";
			a.appendChild(document.createTextNode(outputs[o]));
			td5.appendChild(a);
		}


		//Actions
		var td6 = document.createElement("td");
		td6.style.textAlign = "right";

		//---> Delete
		var deleteA = document.createElement("a");
		deleteA.href = "javascript:;";
		deleteA.style.padding = "4px";
		var deleteImg = document.createElement("img");
		deleteImg.src = "img/silk/delete.png";
		deleteImg.style.border = "0px";
		deleteImg.alt = "Delete Report";
		deleteImg.setAttribute("filename", filename);
		deleteA.appendChild(deleteImg);
		td6.appendChild(deleteA);

		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		tr.appendChild(td5);
		tr.appendChild(td6);

		tbody.appendChild(tr);


		Event.observe(deleteA, "click", confirmDeleteReport);
	}

	//Force tooltip parsing
	parse_tooltips();
}


function deleteParentRow(elm) {
	while (elm && (elm.nodeName.toLowerCase()!="tr") ) {
		elm = elm.parentNode;
	}

	if (elm) {
		elm.parentNode.removeChild(elm);
	}
}

function confirmDeleteReport(evt) {
	var a = Event.findElement(evt);
	if (confirm("Do you really want to delete this report?")) {

		new Ajax.Request("api_delete_file.php?realm=report&file="+a.getAttribute("filename"), {
			method: 'post',
			onSuccess: function(transport) {
				deleteParentRow(a);
				alert("Report was successfully deleted!");
			},
			onFailure: function(transport) {
			},
		});
	}
}

function confirmDeleteTemplate(evt) {
	var a = Event.findElement(evt);
	if (confirm("Do you really want to delete this report template?")) {

		new Ajax.Request("api_delete_file.php?realm=template&file="+a.getAttribute("filename"), {
			method: 'post',
			onSuccess: function(transport) {
				deleteParentRow(a);
				alert("Report template was successfully deleted!");
			},
			onFailure: function(transport) {
			},
		});
	}
}

function fetchTemplates() {

	new Ajax.Request("api_list_templates.php", {
		method: 'get',
		onSuccess: function(transport) {
			renderTemplates(transport.responseXML);
		},
		onFailure: function(transport) {
		},
	});

}

function fetchOutputs() {

	new Ajax.Request("api_list_outputs.php", {
		method: 'get',
		onSuccess: function(transport) {
			outputs = new Array();
			var xmlOutputs = transport.responseXML.getElementsByTagName("output");
			for (var i=0; i<xmlOutputs.length; i++) {
				outputs.push(xmlOutputs[i].getAttribute("title"));
			}
			fetchReports();
		},
		onFailure: function(transport) {
		},
	});

}

function fetchReports() {

	new Ajax.Request("api_list_reports.php?offset="+reportOffset+"&amount="+reportAmount, {
		method: 'get',
		onSuccess: function(transport) {
			clearReports();
			maxReports = parseInt(transport.responseXML.getElementsByTagName("response").item(0).getAttribute("max"));
			renderReports(transport.responseXML);
			repaintPagination();
		},
		onFailure: function(transport) {
		},
	});

}

function repaintPagination() {
	if (reportOffset > 0) {
		$("previous_reports").style.visibility = "visible";
	} else {
		$("previous_reports").style.visibility = "hidden";
	}

	if (reportOffset+reportAmount < maxReports) {
		$("next_reports").style.visibility = "visible";
	} else {
		$("next_reports").style.visibility = "hidden";
	}
}

function previousReports() {
	reportOffset = Math.max(0, reportOffset-reportAmount);
	fetchReports();
}

function nextReports() {
	reportOffset += reportAmount;
	fetchReports();
}

function repaintReports() {
	reportAmount = parseInt($("report_count").options[ $("report_count").options.selectedIndex].value);
	fetchReports();
}

onload = function() {

	body = document.getElementsByTagName("body").item(0);

	fetchTemplates();
	fetchOutputs();

	Event.observe($("report_count"), "change", repaintReports);
	Event.observe($("previous_reports"), "click", previousReports);
	Event.observe($("next_reports"), "click", nextReports);
}